﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic;
using LotteryVote.Logic.Web;
using LotteryVote.Entity;
using LotteryVote.Utility;

namespace LotteryVote
{
    public partial class LotteryCompanyManager : AuthorizePage
    {
        CompanyFacade companyFacade = new CompanyFacade();
        protected void Page_Load(object sender, EventArgs e)
        {
            Execute(() =>
            {
                if (!IsPostBack)
                {
                    BindWeeks();
                    BindCompanys();
                }
            });
        }

        void BindCompanys()
        {
            var companys = companyFacade.GetAllCompany();
            rpt_company.DataSource = companys;
            rpt_company.DataBind();

            ddl_company.DataSource = companys;
            ddl_company.DataTextField = LotteryCompany.NAME;
            ddl_company.DataValueField = LotteryCompany.COMPANYID;
            ddl_company.DataBind();
            ddl_company.Items.Insert(0, new ListItem(GetGlobalResourceObject("Resource", "PleaseSelecte").ToString(), "0"));
        }
        void BindWeeks()
        {
            foreach (var dayOfWeek in EnumHelper.ToList<DayOfWeek>())
            {
                ck_week.Items.Add(new ListItem(GetGlobalResourceObject("Resource", dayOfWeek.ToString()).ToString(), ((int)dayOfWeek).ToString()));
            }
        }
        void BindCompanyCycle()
        {
            Execute(() =>
            {
                int companyId = int.Parse(ddl_company.SelectedValue);
                CompanyLotteryCycleFacade blCompanyCycle = new CompanyLotteryCycleFacade();
                var cycles = blCompanyCycle.GetCycles(companyId);
                foreach (ListItem item in ck_week.Items)
                    item.Selected = false;
                foreach (var cycle in cycles)
                {
                    string dayOfWeek = ((int)cycle.DayOfWeek).ToString();
                    foreach (ListItem item in ck_week.Items)
                        if (item.Value == dayOfWeek)
                            item.Selected = true;
                }
            });
        }

        protected void rpt_company_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Execute(() =>
            {
                if (e.CommandName == "del" && CurrentUser.Role == SystemRole.Company)
                {
                    int companyId = Convert.ToInt32(e.CommandArgument);
                    companyFacade.Remove(companyId);
                    ShowMessage("Result", GetGlobalResourceObject("Resource", "Success").ToString());
                    BindCompanys();
                }
            });
        }

        protected void ddl_company_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_company.SelectedIndex == 0) return;
            BindCompanyCycle();
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            if (ddl_company.SelectedIndex == 0 || CurrentUser.Role != SystemRole.Company) return;
            Execute(() =>
            {
                int companyId = Convert.ToInt32(ddl_company.SelectedValue);
                List<DayOfWeek> days = new List<DayOfWeek>();
                foreach (ListItem item in ck_week.Items)
                    if (item.Selected)
                        days.Add(EnumHelper.GetEnum<DayOfWeek>(item.Value));

                CompanyLotteryCycleFacade blCompanyCycle = new CompanyLotteryCycleFacade();
                blCompanyCycle.UpdateCycles(companyId, days);
                ShowMessage("Result", GetGlobalResourceObject("Resource", "Success").ToString());
            });
        }

        protected void btn_updateTodayCompany_Click(object sender, EventArgs e)
        {
            Execute(() =>
            {
                TodayLotteryCompany.Instance.UpdateTodayLotteryCompany();
                ShowMessage("Result", GetGlobalResourceObject("Resource", "Success").ToString());
            });
        }

    }
}